home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / MOLEHILL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.3 KB  |  154 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1995. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* molehill uses the GLU NURBS routines to draw some nice surfaces. */
  9.  
  10. #include <GL/glut.h>
  11.  
  12. GLfloat mat_red_diffuse[] = { 0.7, 0.0, 0.1, 1.0 };
  13. GLfloat mat_green_diffuse[] = { 0.0, 0.7, 0.1, 1.0 };
  14. GLfloat mat_blue_diffuse[] = { 0.0, 0.1, 0.7, 1.0 };
  15. GLfloat mat_yellow_diffuse[] = { 0.7, 0.8, 0.1, 1.0 };
  16. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  17. GLfloat mat_shininess[] = { 100.0 };
  18. GLfloat knots[8] = { 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 };
  19. GLfloat pts1[4][4][3], pts2[4][4][3];
  20. GLfloat pts3[4][4][3], pts4[4][4][3];
  21. GLUnurbsObj *nurb;
  22. int u, v;
  23.  
  24. static void 
  25. display(void)
  26. {
  27.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  28.   glCallList(1);
  29.   glFlush();
  30. }
  31.  
  32. int 
  33. main(int argc, char **argv)
  34. {
  35.   glutInit(&argc, argv);
  36.   glutCreateWindow("molehill");
  37.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  38.   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  39.   glEnable(GL_LIGHTING);
  40.   glEnable(GL_LIGHT0);
  41.   glEnable(GL_DEPTH_TEST);
  42.   glEnable(GL_AUTO_NORMAL);
  43.   glEnable(GL_NORMALIZE);
  44.   nurb = gluNewNurbsRenderer();
  45.   gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, 25.0);
  46.   gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_FILL);
  47.  
  48.   /* Build control points for NURBS mole hills. */
  49.   for(u=0; u<4; u++) {
  50.     for(v=0; v<4; v++) {
  51.       /* Red. */
  52.       pts1[u][v][0] = 2.0*((GLfloat)u);
  53.       pts1[u][v][1] = 2.0*((GLfloat)v);
  54.       if((u==1 || u == 2) && (v == 1 || v == 2))
  55.     /* Stretch up middle. */
  56.     pts1[u][v][2] = 6.0;
  57.       else
  58.     pts1[u][v][2] = 0.0;
  59.  
  60.       /* Green. */
  61.       pts2[u][v][0] = 2.0*((GLfloat)u - 3.0);
  62.       pts2[u][v][1] = 2.0*((GLfloat)v - 3.0);
  63.       if((u==1 || u == 2) && (v == 1 || v == 2))
  64.     if(u == 1 && v == 1) 
  65.       /* Pull hard on single middle square. */
  66.       pts2[u][v][2] = 15.0;
  67.     else
  68.       /* Push down on other middle squares. */
  69.       pts2[u][v][2] = -2.0;
  70.       else
  71.     pts2[u][v][2] = 0.0;
  72.  
  73.       /* Blue. */
  74.       pts3[u][v][0] = 2.0*((GLfloat)u - 3.0);
  75.       pts3[u][v][1] = 2.0*((GLfloat)v);
  76.       if((u==1 || u == 2) && (v == 1 || v == 2))
  77.     if(u == 1 && v == 2)
  78.       /* Pull up on single middple square. */
  79.       pts3[u][v][2] = 11.0;
  80.     else
  81.       /* Pull up slightly on other middle squares. */
  82.       pts3[u][v][2] = 2.0;
  83.       else
  84.     pts3[u][v][2] = 0.0;
  85.  
  86.       /* Yellow. */
  87.       pts4[u][v][0] = 2.0*((GLfloat)u);
  88.       pts4[u][v][1] = 2.0*((GLfloat)v - 3.0);
  89.       if((u==1 || u == 2 || u == 3) && (v == 1 || v == 2))
  90.     if(v == 1) 
  91.       /* Push down front middle and right squares. */
  92.       pts4[u][v][2] = -2.0;
  93.     else
  94.       /* Pull up back middle and right squares. */
  95.       pts4[u][v][2] = 5.0;
  96.       else
  97.     pts4[u][v][2] = 0.0;
  98.     }
  99.   }
  100.   /* Stretch up red's far right corner. */
  101.   pts1[3][3][2] = 6;
  102.   /* Pull down green's near left corner a little. */
  103.   pts2[0][0][2] = -2;
  104.   /* Turn up meeting of four corners. */
  105.   pts1[0][0][2] = 1;
  106.   pts2[3][3][2] = 1;
  107.   pts3[3][0][2] = 1;
  108.   pts4[0][3][2] = 1;
  109.  
  110.   glMatrixMode(GL_PROJECTION);
  111.   gluPerspective(55.0, 1.0, 2.0, 24.0);
  112.   glMatrixMode(GL_MODELVIEW);
  113.   glTranslatef(0.0, 0.0, -15.0);
  114.   glRotatef(330.0, 1.0, 0.0, 0.0);
  115.  
  116.   glNewList(1, GL_COMPILE);
  117.     /* Render red hill. */
  118.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_red_diffuse);
  119.     gluBeginSurface(nurb);
  120.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  121.         4 * 3, 3, &pts1[0][0][0],
  122.         4, 4, GL_MAP2_VERTEX_3);
  123.     gluEndSurface(nurb);
  124.  
  125.     /* Render green hill. */
  126.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse);
  127.     gluBeginSurface(nurb);
  128.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  129.         4 * 3, 3, &pts2[0][0][0],
  130.         4, 4, GL_MAP2_VERTEX_3);
  131.     gluEndSurface(nurb);
  132.  
  133.     /* Render blue hill. */
  134.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_blue_diffuse);
  135.     gluBeginSurface(nurb);
  136.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  137.         4 * 3, 3, &pts3[0][0][0],
  138.         4, 4, GL_MAP2_VERTEX_3);
  139.     gluEndSurface(nurb);
  140.  
  141.     /* Render yellow hill. */
  142.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_yellow_diffuse);
  143.     gluBeginSurface(nurb);
  144.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  145.         4 * 3, 3, &pts4[0][0][0],
  146.         4, 4, GL_MAP2_VERTEX_3);
  147.     gluEndSurface(nurb);
  148.   glEndList();
  149.  
  150.   glutDisplayFunc(display);
  151.   glutMainLoop();
  152.   return 0;             /* ANSI C requires main to return int. */
  153. }
  154.